package com.realgodo.site.service.application;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.hibernate.DefaultEntityManager;

import com.realgodo.site.entity.application.mood.Mood;

import edu.emory.mathcs.backport.java.util.Arrays;

@Service
@Transactional
public class MoodManager extends DefaultEntityManager<Mood, Long> {
	@Autowired
	protected SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	public Page<Mood> find(int pageSize, Map<String, Object> param) {
		Page<Mood> page = new Page<Mood>(pageSize);
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Mood.class);
		if (param == null) {
			page.setPageNo(1);
			return this.entityDao.findByCriteria(page, c);
		}

		page.setPageNo(super.isParamNotNull(param, Page.PAGE) ? Integer
				.valueOf((String) getParamValue(param, Page.PAGE)) : 1);// 查询页数

		if (isParamNotNull(param, "spaceIds")) {
			// c.createAlias("space", "space");
			Object value = getParamValue(param, "spaceIds");
			if (value instanceof String) {
				c.add(Restrictions.eq("space.id", Long
						.valueOf((String) getParamValue(param, "spaceIds"))));
			} else if (value instanceof List) {
				c.add(Restrictions.in("space.id", (List<Long>) getParamValue(
						param, "spaceIds")));
			} else if (value instanceof String[]) {
				c.add(Restrictions.in("space.id", (List<Long>) Arrays
						.asList((String[]) getParamValue(param, "spaceIds"))));
			}
		} else if (isParamNotNull(param, "spaceId")) {
			// c.createAlias("space", "space");
			c.add(Restrictions.eq("space.id", Long
					.valueOf((String) getParamValue(param, "spaceId"))));
		}
		c.addOrder(Order.desc("id"));
		return this.entityDao.findByCriteria(page, c);
	}

	/** 发布/更新纠结 */
	public Mood saveMood(Mood mood) {
		if (mood == null) {
			return null;
		}
		String content = mood.getContent();
		if (mood.getId() != null && !Long.valueOf(-1).equals(mood.getId())) {
			mood = this.get(mood.getId());
			mood.setUpdateTime(new Date());
		} else {
			mood.setPubTime(new Date());
		}
		mood.setClasz(Mood.class);
		mood.setContent(ApplicationManager.convert(content));
		this.save(mood);
		return mood;
	}

	@SuppressWarnings("unchecked")
	public Mood getLastMood(Long aSpaceId) {
		Session _session = sessionFactory.getCurrentSession();
		Query _hql = _session
				.createQuery("select  o from Mood o where o.space.id = ? order by o.id desc");
		_hql.setParameter(0, aSpaceId);
		_hql.setMaxResults(1);
		List<Mood> _list = _hql.list();
		return (!_list.isEmpty()) ? _list.get(0) : null;
	}
	
	public Long getMoodCount(Long spaceId) {
		Session session = this.sessionFactory.getCurrentSession();
		String hql = "select count(o) from Mood o where o.space.id = "
				+ spaceId;
		Query q = session.createQuery(hql);
		return (Long) q.uniqueResult();
	}
}
